package com.liunian.subject.domain.handler.subject;

import com.liunian.subject.common.enums.IsDeletedFlagEnum;
import com.liunian.subject.common.enums.SubjectTypeEnum;
import com.liunian.subject.domain.entity.SubjectInfoBO;
import com.liunian.subject.domain.entity.SubjectOptionBO;
import com.liunian.subject.infra.basic.entity.SubjectJudge;
import com.liunian.subject.infra.basic.service.SubjectJudgeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Component
public class SubjectJudgeHandler implements SubjectInfoTypeHandler{

    @Resource
    private SubjectJudgeService subjectJudgeService;

    @Override
    public SubjectTypeEnum getSubjectType() {
        return SubjectTypeEnum.JUDGE;
    }

    @Override
    public Boolean add(SubjectInfoBO subjectInfoBO) {
        SubjectJudge subjectJudge = new SubjectJudge();
        SubjectInfoBO.SubjectAnswerBO subjectAnswerBO = subjectInfoBO.getOptionList().get(0);
        subjectJudge.setSubjectId(subjectInfoBO.getId());
        subjectJudge.setIsCorrect(subjectAnswerBO.getIsCorrect());
        subjectJudge.setCreatedAt(LocalDateTime.now());
        subjectJudge.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        return subjectJudgeService.saveJudge(subjectJudge);
    }

    @Override
    public SubjectOptionBO query(long subjectId) {
        List<SubjectJudge> subjectJudges = subjectJudgeService.selectJudgeAnswer(subjectId);
        List<SubjectInfoBO.SubjectAnswerBO> answerBOS = new ArrayList<>();
        subjectJudges.forEach(data -> {
            SubjectInfoBO.SubjectAnswerBO bo = new SubjectInfoBO.SubjectAnswerBO();
            bo.setIsCorrect(data.getIsCorrect());
            answerBOS.add(bo);
        });
        SubjectOptionBO bo = new SubjectOptionBO();
        bo.setOption(answerBOS);
        return bo;
    }
}
